package top.humbleyuan.datastructure.linkedlist;

/**
 * @Author HumbleYuan
 * @Date 2019/10/10 15:27
 * @Des LinkList包装类 link707
 */
public class MyLinkedList {
    public LinkNode first ;      //头结点
    int length ;

    /** Initialize your data structure here. */
    public MyLinkedList() {
        length = getLength();
    }

    /** Get length of this linked list */
    public int getLength() {
        LinkNode temp = first;

        int length = 0;
        while (temp != null) {
            temp = temp.next;
            length ++;
        }
        return length;
    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        int curIndex = 0;
        if(first == null)
            return -1;
        LinkNode temp = first;
        while(curIndex < index && temp.next != null){
            temp = temp.next;
            curIndex++;
        }

        if(index < 0 || curIndex < index)
            return -1;
        return temp.val;
    }

    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        length ++;
        if(first == null) {
            first = new LinkNode(val);
            return;
        }

        LinkNode newFirst = new LinkNode(val);
        newFirst.next = first;
        first = newFirst;
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        length ++;
        if(first == null) {
            first = new LinkNode(val);
            return;
        }

        LinkNode temp = first;
        while(temp.next != null) {
            temp = temp.next;
        }
        LinkNode last = new LinkNode(val);
        temp.next = last;
        last.next = null;
    }

    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        if(index > length)
            return;
        if(index <= 0) {
            addAtHead(val);
            return;
        }

        LinkNode temp = first.next;
        LinkNode preTemp = first;
        int curIndex = 1;
        while (curIndex < index && temp.next != null) {
            preTemp = temp;
            temp = temp.next;

            curIndex ++;
        }

        LinkNode insertNode = new LinkNode(val);
        insertNode.val = val;
        if(index - curIndex == 1) {
            temp.next = insertNode;
            insertNode.next = null;
        }else {
            preTemp.next = insertNode;
            insertNode.next = temp;
        }
        length ++;
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if(first == null || index >= length)
            return;

        if(index == 0) {
           first = first.next;
           return;
        }

        LinkNode preTemp = first;
        LinkNode temp = first.next;
        int curIndex = 1;
        while(curIndex < index && temp.next != null) {
            preTemp = temp;
            temp = temp.next;
            curIndex ++;
        }

        preTemp.next = temp.next;
    }
}
